<template>
  <div>
    <el-card class="box-card">
      <el-button
        type="primary"
        size="default"
        icon="Plus"
        @click="addTrademark"
      >
        添加品牌
      </el-button>
      <el-table stripe border style="margin: 10px 0px" :data="trademarkList">
        <el-table-column type="index" align="center" label="序号" width="80" />
        <el-table-column prop="tmName" label="品牌名称" />
        <el-table-column prop="logoUrl" label="品牌logo">
          <!-- 作用域插槽 -->
          <template #="{ row, $index }">
            <img :src="row.logoUrl" alt="" style="height: 40px" />
          </template>
        </el-table-column>
        <el-table-column label="品牌操作">
          <template #="{ row, $index }">
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              @click="updateTrademark"
            />
            <el-button type="danger" size="small" icon="Delete" />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        @current-change="changePageNo"
        @size-change="sizeChange"
        :page-sizes="[10, 20, 30, 40, 50]"
        :small="true"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
      />
    </el-card>
    <el-dialog v-model="dialogFormVisible" title="添加品牌">
      <el-form style="width: 80%" label-width="120px">
        <el-form-item label="品牌名称">
          <el-input
            type="text"
            placeholder="请输入品牌名称"
            v-model="trademarkParams.tmName"
          />
        </el-form-item>
        <el-form-item label="品牌LOGO">
          <!-- action图片上传路径写/api，代理服务器不发送这次post请求 -->
          <el-upload
            class="avatar-uploader"
            action="/api/admin/product/fileUpload"
            :show-file-list="true"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="trademarkParams.logoUrl"
              :src="trademarkParams.logoUrl"
              class="avatar"
            />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="default" @click="cancel">
          取消
        </el-button>
        <el-button type="primary" size="default" @click="confirm">
          确认
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { reqHasTrademark } from '@/api/product/trademark'
import { ElMessage } from 'element-plus'
import type { UploadProps } from 'element-plus'
import type {
  Records,
  Trademark,
  TrademarkResponseData,
} from '@/api/product/trademark/type'
const pageNo = ref<number>(1)
const pageSize = ref<number>(10)
const total = ref<number>(0)
let trademarkList = ref<Records>([])

const getHasTrademark = async () => {
  const result: TrademarkResponseData = await reqHasTrademark(
    pageNo.value,
    pageSize.value,
  )
  console.log(result)
  total.value = result.data.total
  trademarkList.value = result.data.records
}
onMounted(() => {
  getHasTrademark()
})

// 翻页
const changePageNo = () => {
  getHasTrademark()
}
// 展示条数改变
const sizeChange = () => {
  pageNo.value = 1
  getHasTrademark()
}

const dialogFormVisible = ref<boolean>(false)
const addTrademark = () => {
  dialogFormVisible.value = true
}
const updateTrademark = () => {
  dialogFormVisible.value = true
}
const cancel = () => {
  dialogFormVisible.value = false
}
const confirm = () => {
  dialogFormVisible.value = false
}
// 图片上传
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response, // 请求成功后，服务器返回的数据
  uploadFile,
) => {
  trademarkParams.logoUrl = response.data
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (
    rawFile.type === 'image/jpeg' ||
    rawFile.type === 'image/png' ||
    rawFile.type === 'image/gif'
  ) {
    if (rawFile.size / 1024 / 1024 < 2) {
      return true
    } else {
      ElMessage.error('上传的文件不能大于2M!')
      return true
    }
  } else {
    ElMessage.error('上传的文件格式务必是JPG|PNG|GIF!')
    return false
  }
}

// 新增品牌数据
let trademarkParams = reactive<Trademark>({
  tmName: '',
  logoUrl: '',
})
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
